package cn.gemiman.daoyi.module.cms.controller.admin.appsite.vo;

import cn.gemiman.daoyi.framework.common.enums.TerminalEnum;
import cn.gemiman.daoyi.framework.common.validation.InEnum;
import cn.gemiman.daoyi.module.cms.dal.dataobject.appsite.AppSiteDO;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.AssertTrue;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.Data;

import java.util.List;

@Schema(description = "管理后台 - 站点新增/修改 Request VO")
@Data
public class AppSiteSaveReqVO {

    @Schema(description = "编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "28630")
    private Long id;

    @Schema(description = "站点类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "2")
    @NotNull(message = "站点类型不能为空")
    @InEnum(TerminalEnum.class)
    private Integer terminalType;

    @Schema(description = "站点标识", requiredMode = Schema.RequiredMode.REQUIRED, example = "8328")
    private String appId;

    @Schema(description = "站点名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "张三")
    @NotEmpty(message = "站点名称不能为空")
    private String appName;

    @Schema(description = "LOGO", example = "https://www.iocoder.cn")
    private String appLogoUrl;

    @Schema(description = "站点相册")
    private List<String> appPhotos;

    @Schema(description = "站点简介")
    private String appShortDesc;

    @Schema(description = "站点介绍", example = "随便")
    private String appDescription;

    @Schema(description = "站点配置")
    private List<AppSiteDO.AppSiteConfigItem> configInfoList;


    @AssertTrue(message = "站点配置有误")
    @JsonIgnore
    public boolean isValidConfigInfoList() {
        if (CollUtil.isEmpty(configInfoList)) {
            return true;
        }
        if (configInfoList.size() != configInfoList.stream().map(AppSiteDO.AppSiteConfigItem::getKey).distinct().count()) { // 防止攻击
            return false;
        }
        return configInfoList.stream().noneMatch(item -> {
            if (StrUtil.isBlank(item.getKey()) || null == item.getType() || null == item.getValue()) {
                return true;
            }
            return switch (item.getType()) {
                case 0, 1, 4 -> // 字符串
                        !(item.getValue() instanceof String) || StrUtil.isBlank((String) item.getValue());
                case 2, 3 -> // 多图
                        !(item.getValue() instanceof List) || CollUtil.isEmpty((List<?>) item.getValue());
                default -> true;
            };
        });
    }

    /**
     * 绑定域名
     */
    @Schema(description = "绑定域名")
    private String bindDomain;
    /**
     * 用户类型
     */
    private Integer userType;
    /**
     * 用户编号
     */
    private Long userId;
    /**
     * 用户昵称
     */
    private String userNickname;
    /**
     * 用户头像
     */
    private String userAvatar;
    /**
     * 评论状态
     * <p>
     * 枚举 {@link TODO common_status 对应的类}
     */
    private Integer status;
    /**
     * 审核状态
     * <p>
     * 枚举 {@link TODO bpm_process_instance_status 对应的类}
     */
    private Integer approveStatus;
}
